<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\WechatoffiaccountService;
use App\Services\SmsService;
use App\Models\Memberorders;

class Membernotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'member:notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '会员到期提醒(9:00)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $threeday = strtotime("+3 days");
        $twoday = strtotime("+4 days");
        $starttime = strtotime(date("Y-m-d",$threeday));
        $endtime = strtotime(date("Y-m-d",$twoday));
        $products = ["","免费版","基础版","高级版","旗舰版"];
        $orders = Memberorders::getExpiredMember($starttime,$endtime);

        $offapp = WechatoffiaccountService::officialAccount();
        foreach ($orders as $key => $order) {
            $end_time = date("Y-m-d H:i:s",$order->end_time);
            $end_date = date("Y-m-d",$order->end_time);
            // 发送短信通知
            if (!empty($order->username)) {
                SmsService::memberMotice($order->username,$end_date);
            }
            // 发送公众号推送
            if (!empty($order->wechat_openid)) {
                $offapp->template_message->send([
                    'touser' => $order->wechat_openid,
                    'template_id' => 'jFPtZd4bPUPrJD016VKkhgNIZaQJ0OwgBKUMAALwxVg',
                    'url' => 'https://bab.moreqifu.cn/admin/prices/products_lists_pay',
                    'miniprogram' => [
                        'appid' => 'wx820e151d0905ab1a',
                        'pagepath' => 'pages/users/users_member',
                    ],
                    'data' => [
                        'first' => '会员3天后到期提醒',
                        'keyword1' => '天天外链',
                        'keyword2' => '天天外链会员('.$products[$order->product_id].')',
                        'keyword3' => $end_time,
                        'remark' => '按季续费8.5折,按年续费5.5折优惠! 已续费请忽略！',
                    ],
                ]);
            }
        }
        return 0;
    }
}
